﻿/*
Copyright (c) 2013, SonicLing
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, 
are permitted provided that the following conditions are met:

    Redistributions of source code must retain the above copyright notice, this 
list of conditions and the following disclaimer.
    Redistributions in binary form must reproduce the above copyright notice, 
this list of conditions and the following disclaimer in the documentation and/or 
other materials provided with the distribution.
    Neither the name of the SonicLing nor the names of its contributors may be 
used to endorse or promote products derived from this software without specific 
prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using XmlConfiguration;
using System.Reflection;

namespace Bibtex2Word
{
	[XmlPrefix("b", Namespace = "http://schemas.openxmlformats.org/officeDocument/2006/bibliography")]
	[XmlRoot("Sources", Namespace = "http://schemas.openxmlformats.org/officeDocument/2006/bibliography")]
	public class WordBibSources : IXmlSerializable
	{
		public System.Xml.Schema.XmlSchema GetSchema()
		{
			throw new NotImplementedException();
		}

		public List<WordBibSource> Sources = new List<WordBibSource>();

		public void ReadXml(XmlReader reader)
		{
			reader.ReadStartElement("b:Sources");
			while (reader.MoveToContent() == XmlNodeType.Element)
			{
				WordBibSource source = new WordBibSource();
				source.ReadXml(reader);
				Sources.Add(source);
			}
			reader.ReadEndElement();
		}

		public void WriteXml(XmlWriter writer)
		{
			foreach (WordBibSource source in Sources)
			{
				writer.WriteStartElement("b:Source");
				source.WriteXml(writer);
				writer.WriteEndElement();
			}
		}
	}

	public enum SourceTypes 
	{
		Book,
		BookSection,
		JournalArticle,
		ArticleInAPeriodical,
		Report,
		Patent,
		Art,
		ConferenceProceedings,
		Interview,
		ElectronicSource,
		InternetSite,
		DocumentFromInternetSite,
		Misc,
	}

	[XmlPrefix("b")]
	[XmlRoot("Source")]
	public class WordBibSource : WordBibXml<WordBibSource>
	{
		public String Tag;
		public SourceTypes SourceType;
		public Guid Guid;
		public WordBibAuthor Author;
		public String Title;
		public String JournalName;
		public String PeriodicalTitle;
		public String BookTitle;
		public int LCID = 1033; // default : US
		public int RefOrder;
		public String Volume;
		public String Year;
		public String Month;
		public String Day;
		public String ConferenceName;
		public String City;
		public String Pages;
		public String Publisher;
		public String Edition;
		public String Issue;
		public String ShortTitle;
		public String StandardNumber;
		public String PatentNumber;
		public String ProductionCompany;
		public String CountryRegion;
		public String Institution;
		public String URL;
		public String YearAccessed;
		public String MonthAccessed;
		public String DayAccessed;
		public String Comments;
	}

	public class WordBibAuthor : WordBibXml<WordBibAuthor>
	{
		public WordBibNameList Author;
		public WordBibNameList Editor;
		public WordBibNameList Writer;
		public WordBibNameList Composer;
		public WordBibNameList Artist;
		public WordBibNameList Director;
		public WordBibNameList Interviewee;
		public WordBibNameList Inventor;

		public override string ToString()
		{
			FieldInfo[] fields = GetType().GetFields(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly);
			foreach (FieldInfo field in fields)
			{
				if (field.FieldType.Equals(typeof(WordBibNameList)) &&
					field.GetValue(this) != null)
				{
					return (field.GetValue(this) as WordBibNameList).ToString();
				}
			}
			return "";
		}
	}

	public class WordBibNameList : WordBibXml<WordBibNameList>
	{
		public WordBibPersons NameList;

		public override string ToString()
		{
			return NameList.ToString();
		}
	}

	public class WordBibPersons : WordBibXml<WordBibPersons>
	{
		public List<WordBibPerson> Person;

		public override string ToString()
		{
			StringBuilder str = new StringBuilder();
			int count = 0;
			foreach (WordBibPerson person in Person)
			{
				if (count++ < 3)
				{
					str.AppendFormat("{0}, ", person);
				}
				else
				{
					str.Append("et al");
				}
			}
			if (str.ToString().EndsWith(", "))
			{
				str.Remove(str.Length - 2, 2);
			}
			return str.ToString();
		}
	}

	public class WordBibPerson : WordBibXml<WordBibPerson>
	{
		public String Last;
		public String Middle;
		public String First;

		public override string ToString()
		{
			return string.Format(string.IsNullOrEmpty(Middle) ? "{0} {1}{2}" : "{0} {1} {2}", Last, InitialChar(Middle), InitialChar(First));
		}

		private string InitialChar(string name)
		{
			if (string.IsNullOrEmpty(name))
			{
				return "";
			}
			return string.Format("{0}.", name.ToUpper()[0]);
		}
	}
}
